import Vue from 'vue'
import Router from 'vue-router'

import Login from '@/components/Login' //登录组件
import Home from '@/components/Home' //后台主页

import Welcome from '@/components/Welcome'//欢迎页面
import Users from '@/components/user/Users'//用户列表组件

import Rights from '@/components/power/Rights'//权限列表组件
import Roles from '@/components/power/Roles'//角色列表组件

import Cate from '@/components/goods/Cate'//商品分类组件
import Params from '@/components/goods/Params'//商品分类组件
import List from '@/components/goods/List'//商品列表组件
import Add from '@/components/goods/Add'//添加商品组件

import Order from '@/components/order/Order'//订单管理组件

import Report from '@/components/report/Report'//数据统计组件

Vue.use(Router)

const router = new Router({
    routes: [{
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        component: Login
    },
    {
        path: '/home',
        component: Home,
        redirect: '/welcome',//访问了home之后重定向到welcome
        children: [
            {
                path: '/welcome',
                component: Welcome
            },
            {
                path: '/users',
                component: Users
            }
            ,
            {
                path: '/rights',
                component: Rights
            },
            {
                path: '/roles',
                component: Roles
            },
            {
                path: '/categories',
                component: Cate
            },
            {
                path: '/params',
                component: Params
            }
            ,
            {
                path: '/goods',
                component: List
            } ,
            {
                path: '/goods/add',
                component: Add
            },
            {
                path: '/orders',
                component: Order
            }
            ,
            {
                path: '/reports',
                component: Report
            }
        ]
    }
    ]
})

//挂载路由导航守卫
router.beforeEach((to, from, next) => {
    //to将要访问的路径
    //from 代表从哪个路径跳转而来
    //next()是一个函数，表示放行

    if (to.path === '/login') return next(); //要访问的是登录界面，直接放行

    //获取token
    const token = sessionStorage.getItem('token');
    if (!token) return next('/login'); //如果没有token，跳转到登录界面

    next(); //有token，放行
})


export default router